package com.slicktechnologies.client.views.contract;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.DatePickerCell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.cell.client.TextInputCell;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.RowCountChangeEvent;
import com.simplesoftwares.client.library.appstructure.SuperTable;
import com.simplesoftwares.client.library.appstructure.UiScreen;
import com.slicktechnologies.client.utility.AppUtility;
import com.slicktechnologies.shared.SalesLineItem;
import com.slicktechnologies.shared.common.productlayer.ItemProduct;
import com.slicktechnologies.shared.common.productlayer.ServiceProduct;
import com.slicktechnologies.shared.common.productlayer.SuperProduct;


public class SalesLineItemTable extends SuperTable<SalesLineItem>  
{
	Column<SalesLineItem,String> deleteColumn,prodNameColumn,modelNoColumn,brandColumn,catColumn,serviceColumn,
	quantColumn,priceColumn,srNo;
	Column<SalesLineItem,Date>dateColumn;
	
	
	TextColumn<SalesLineItem> viewProdName,viewModelNo,viewBrand,viewCat,viewQty,
	viewPriceColumn,viewDateColumn,viewService,totalColumn,viewSrno;



	public SalesLineItemTable()
	{
		super();
	}
	public SalesLineItemTable(UiScreen<SalesLineItem> view)
	{
		super(view);	
	}

	public void createTable()
	{
		createColumnViewCatColumn();
		createColumnViewBrand();
		createColumnEditSrNo();
		createColumnEditprodNameColumn();
		createColumnEditModelNoColumn();
	
		createColumndateColumn();
		createColumnpriceColumn();
		createColumnserviceColumn();
		createColumntotalColumn();
		createColumndeleteColumn();
		
		addFieldUpdater();
		table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);	
		}
	
	
	

	
	
	
	
	protected void createColumnViewprodNameColumn()
	{
		
		viewProdName=new TextColumn<SalesLineItem>()
				{
			@Override public String getValue(SalesLineItem object){
				return object.getProductName();
			}
				};
				table.addColumn(viewProdName,"Product Name");

	}
	
	protected void createColumnEditSrNo()
	{
		TextInputCell cell=new TextInputCell();
		srNo=new Column<SalesLineItem,String>(cell)
				{
			@Override public String getValue(SalesLineItem object){
				return object.getSrNo();
			}
				};
				table.addColumn(srNo,"Serial Number");

	}
	
	protected void createFieldUpdaterSerialNumber()
	{
		srNo.setFieldUpdater(new FieldUpdater<SalesLineItem, String>() {

			@Override
			public void update(int index, SalesLineItem object, String value) {
				
				object.setSrNo(value);
			}
		});

	}
	
	
	
	
	
	protected void createColumnViewSrNo()
	{
		
		viewSrno=new TextColumn<SalesLineItem>()
				{
			@Override public String getValue(SalesLineItem object){
				return object.getSrNo();
			}
				};
				table.addColumn(viewSrno,"Serail Number");

	}
	
	//****************************************************************************************************************//
	protected void createColumnEditprodNameColumn()
	{
		
		TextInputCell cell=new TextInputCell();
		prodNameColumn=new Column<SalesLineItem,String>(cell)
				{
			@Override public String getValue(SalesLineItem object){
				return object.getProductName();
			}
				};
				table.addColumn(prodNameColumn,"Product Name");

	}
	
	protected void createFieldUpdaterProductName()
	{
		prodNameColumn.setFieldUpdater(new FieldUpdater<SalesLineItem, String>() {

			@Override
			public void update(int index, SalesLineItem object, String value) {
				
				object.setProductName(value);
			}
		});

	}
	
	
	
	
	
	protected void createColumnViewModelNo()
	{
		
		viewModelNo=new TextColumn<SalesLineItem>()
				{
			@Override public String getValue(SalesLineItem object){
				return object.getModelNo();
			}
				};
				table.addColumn(viewModelNo,"Model No");

	}
	
	
	
	
	
	
	
	
	
	
	
	
	protected void createFieldUpdaterModelNo()
	{
		modelNoColumn.setFieldUpdater(new FieldUpdater<SalesLineItem, String>() {

			@Override
			public void update(int index, SalesLineItem object, String value) {
				
				object.setModelNo(value);
			}
		});

	}
	
	protected void createColumnEditModelNoColumn()
	{
		TextInputCell cell=new TextInputCell();
		modelNoColumn=new Column<SalesLineItem,String>(cell)
				{
			@Override public String getValue(SalesLineItem object){
				return object.getModelNo();
			}
				};
		table.addColumn(modelNoColumn,"Model Number");

	}
	
	
	
	protected void createColumnViewBrand()
	{
		
		viewBrand=new TextColumn<SalesLineItem>()
				{
			@Override public String getValue(SalesLineItem object){
				return object.getBrand();
			}
				};
				table.addColumn(viewBrand,"Brand");

	}
	
	protected void createFieldUpdaterBrand()
	{
		modelNoColumn.setFieldUpdater(new FieldUpdater<SalesLineItem, String>() {

			@Override
			public void update(int index, SalesLineItem object, String value) {
				
				object.setBrand(value);
			}
		});

	}
	
	protected void createColumnEditCatColumn()
	{
		TextInputCell cell=new TextInputCell();
		catColumn=new Column<SalesLineItem,String>(cell)
				{
			@Override public String getValue(SalesLineItem object){
				return object.getCat();
			}
				};
		table.addColumn(catColumn,"Category");

	}
	
	protected void createFieldUpdaterCategory()
	{
		catColumn.setFieldUpdater(new FieldUpdater<SalesLineItem, String>() {

			@Override
			public void update(int index, SalesLineItem object, String value) {
				object.setCat(value);
				
			}
		});

	}
	
	
	protected void createColumnViewCatColumn()
	{
		
		viewCat=new TextColumn<SalesLineItem>()
				{
			@Override public String getValue(SalesLineItem object){
				return object.getCat();
			}
				};
				table.addColumn(viewCat,"Category");

	}
	
	protected void createFieldUpdaterCat()
	{
		viewCat.setFieldUpdater(new FieldUpdater<SalesLineItem, String>() {

			@Override
			public void update(int index, SalesLineItem object, String value) {
				
				object.setCat(value);
			}
		});

	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	protected void createColumnprodQtyColumn()
	{
		EditTextCell editCell=new EditTextCell();
		quantColumn=new Column<SalesLineItem,String>(editCell)
				{
			@Override
			public String getValue(SalesLineItem object)
			{
				if(object.getQuantity()==0){
					return 0+"";}
				else{
					return object.getQuantity()+"";}
			}
				};
				table.addColumn(quantColumn,"#Qty");
               
	}
	
	
	protected void createViewQtyColumn()
	{
		
		viewQty=new TextColumn<SalesLineItem>()
				{
			@Override
			public String getValue(SalesLineItem object)
			{
				if(object.getQuantity()==0){
					return 0+"";}
				else{
					return object.getQuantity()+"";}
			}
				};
				table.addColumn(viewQty,"#Qty");
	}
	
	
	protected void createColumndateColumn()
	{
		DateTimeFormat fmt = DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT);
		DatePickerCell date= new DatePickerCell(fmt);
		dateColumn=new Column<SalesLineItem,Date>(date)
				{
			@Override
			public Date getValue(SalesLineItem object)
			{
				if(object.getExpectedDelDate()!=null){
					return object.getExpectedDelDate();}
				else{object.setExpectedDelDate(new Date());
				return new Date();
				}
			}
				};
				table.addColumn(dateColumn,"Date");
	}
	
	protected void createViewColumndateColumn()
	{
		
		
		viewDateColumn=new TextColumn<SalesLineItem>()
				{
			@Override
			public String getValue(SalesLineItem object)
			{
				if(object.getExpectedDelDate()!=null)
				{
					
					return AppUtility.parseDate(object.getExpectedDelDate());}
				else{
				return "N.A.";
				}
			}
				};
				table.addColumn(viewDateColumn,"Date");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	protected void createColumnpriceColumn()
	{
		EditTextCell editCell=new EditTextCell();
		priceColumn=new Column<SalesLineItem,String>(editCell)
				{
			@Override
			public String getValue(SalesLineItem object)
			{
				return object.getPrice()+"";
			}
				};
				table.addColumn(priceColumn,"#Price");
	}
	
	protected void createViewColumnpriceColumn()
	{
		
		viewPriceColumn=new TextColumn<SalesLineItem>()
				{
			@Override
			public String getValue(SalesLineItem object)
			{
				if(object.getPrice()==0){
					return 0+"";}
				else{
					return object.getPrice()+"";}
			}
				};
				table.addColumn(viewPriceColumn,"#Price");
	}
	
	
	
	
	
	
	
	
	
	protected void createColumnserviceColumn()
	{
		TextInputCell editCell=new TextInputCell();
		serviceColumn=new Column<SalesLineItem,String>(editCell)
				{
			@Override
			public String getValue(SalesLineItem object)
			{
				
				return object.getServiceTax()+"";
			}
				};
				table.addColumn(serviceColumn,"#Service Tax");
	}
	
	protected void createFiledUpdaterService()
	{
		serviceColumn.setFieldUpdater(new FieldUpdater<SalesLineItem, String>() {

			@Override
			public void update(int index, SalesLineItem object, String value) {
				double serTax;
				try{
					serTax=Double.parseDouble(value.trim());
					object.setServiceTax(serTax);
				}
				catch(Exception e)
				{
					
				}
				
			     
				
			}
		});
	}
	
	
	protected void createViewColumnserviceColumn()
	{
		
		viewService=new TextColumn<SalesLineItem>()
				{
			@Override
			public String getValue(SalesLineItem object)
			{
				
				return object.getServiceTax()+"";
			}
				};
				table.addColumn(serviceColumn,"#Service Tax");
	}
	
	
	
	protected void createColumntotalColumn()
	{
		
		totalColumn=new TextColumn<SalesLineItem>()
				{
			@Override
			public String getValue(SalesLineItem object)
			{
				
				double tax=object.getServiceTax();
				double total=(tax+object.getPrice())*object.getQuantity();
				
				
				return total+"";
			}
				};
				table.addColumn(totalColumn,"Total");
	}
	protected void createColumndeleteColumn()
	{
		ButtonCell btnCell= new ButtonCell();
		deleteColumn=new Column<SalesLineItem,String>(btnCell)
				{
			@Override
			public String getValue(SalesLineItem object)
			{
				return  "Delete" ;
			}
				};
				table.addColumn(deleteColumn,"Delete");
	}



	@Override public void addFieldUpdater() 
	{
		
		createFieldUpdaterdateColumn();
		createFieldUpdaterpriceColumn();
		createFieldUpdaterBrand();
		createFieldUpdaterProductName();
		createFiledUpdaterService();
		createFieldUpdaterModelNo();
		
		createFieldUpdaterdeleteColumn();
		createFieldUpdaterSerialNumber();
		


	}

	
	protected void createFieldUpdaterdateColumn()
	{
		dateColumn.setFieldUpdater(new FieldUpdater<SalesLineItem, Date>()
				{
			@Override
			public void update(int index,SalesLineItem object,Date value)
			{
				object.setExpectedDelDate(value);
				table.redrawRow(index);
			}
				});
	}

	

	


	protected void createFieldUpdaterpriceColumn()
	{
		priceColumn.setFieldUpdater(new FieldUpdater<SalesLineItem, String>()
				{
			@Override

			public void update(int index,SalesLineItem object,String value)
			{
				try
				{
					Double val1=Double.parseDouble(value.trim());
			
				object.setPrice(val1);
				RowCountChangeEvent.fire(table, getDataprovider().getList().size(), true);
				
				}
				catch(Exception e)
				{
				}
				table.redrawRow(index);
			}
				});
	}
	protected void createFieldUpdaterdeleteColumn()
	{
		deleteColumn.setFieldUpdater(new FieldUpdater<SalesLineItem,String>()
				{
			@Override
			public void update(int index,SalesLineItem object,String value)
			{
				getDataprovider().getList().remove(object);

				table.redrawRow(index);
			}
				});
	}


	

	
	
	@Override
	public void setEnable(boolean state)
	{
          
		
        for(int i=table.getColumnCount()-1;i>-1;i--)
    	  table.removeColumn(i); 
        	  
      
          if(state==true)
          {
        	  createColumnViewCatColumn();
      		  createColumnViewBrand();
      		  createColumnEditSrNo();
      		  createColumnEditprodNameColumn();
      		  createColumnEditModelNoColumn();
      		  createColumndateColumn();
      		  createColumnpriceColumn();
      		  createColumnserviceColumn();
      		  createColumntotalColumn();
      		  createColumndeleteColumn();
      		  addFieldUpdater();
      		}
 
          else
          {
        	
        	  createColumnViewCatColumn();
        	   createColumnViewBrand();
        	   createColumnViewSrNo();
        	   createColumnViewprodNameColumn();
        	   
        	   createColumnViewBrand();
        	   createColumnViewModelNo();
        	   
        	   createViewColumndateColumn();
        	   createColumntotalColumn();
        	   
          }
	}

	@Override
	public void applyStyle()
	{

	}
	@Override
	protected void initializekeyprovider() {
		keyProvider= new ProvidesKey<SalesLineItem>()
				{
			@Override
			public Object getKey(SalesLineItem item)
			{
				if(item==null)
				{
					return null;
				}
				else
					return 900l;
			}
				};
	}


	

	public double calculateTotal()
	{
		List<SalesLineItem>list=getDataprovider().getList();

		double sum=0;
		for(int i=0;i<list.size();i++)
		{
			SalesLineItem entity=list.get(i);
			
			double tax=entity.getServiceTax();
			sum=sum+(entity.getPrice()+tax)*entity.getQuantity();
	
		}
		
		return sum;

	}
}
